﻿var form, $, table, layer;
layui.use(['element', 'layer', 'form', 'laypage'], function() {
	form = layui.form;
	$ = layui.jquery;
	layer = layui.layer;
	laypage = layui.laypage;
	SearchFulieList(1, 1, 15);
	//添加数据
	$('#btnAdd').click(function() {
		var index = layer.open({
			type: 2,
			title: '福利编辑',
			shadeClose: true,
			shade: 0.8,
			area: ['100%', '100%'],
			content: 'fuli_add.html', //iframe的url
			success: function(layero, index) {
				
			},
			end: function() {

			}
		});
	});
	//查询
	$('#btnSearch').click(function() {
		SearchFulieList(0, 1, 15);
	});

	//获取福利
	function SearchFulieList(type, pg, sz) {
		var index = layer.load(0, {
			shade: false,
			offset: ['100px']
		}); //0代表加载的风格，支持0-2
		var data1 = {
			page: pg,
			size: sz,
		}
		$.ajax({
			url: apiUrl + 'api/admin/SearchFulieList',
			type: 'post', //GET
			async: true, //或false,是否异步
			dataType: 'json',
			contentType: 'application/json',
			data: JSON.stringify(data1),
			headers: {
				'Authorization': 'Bearer ' + sessionStorage.getItem("token")
			},
			success: function(jsondata) {
				if(jsondata.Type == 1) {
					if(type == 1) {
						laypage.render({
							elem: 'page',
							count: jsondata.Count,
							limit: 15,
							layout: ['prev', 'page', 'next', 'count'],
							jump: function(obj, first) {
								if(!first) {
									SearchFulieList(0, obj.curr, obj.limit);
								}
							}
						});
					}
					$("#ulmain").html("");
					$.each(jsondata.Data, function(index, item) {
						var lihtml = '';
						lihtml += '<li class="liimg layui-col-xs6 layui-col-sm4 layui-col-md3">';
						lihtml += '<div class="layui-card">';
						lihtml += '<div class="layui-card-header">' + item.Title + '</div>';
						lihtml += '<div class="layui-card-header">' + item.Createtime.substring(0, 10) + ' <div class="layui-btn-group" >';
						lihtml += '<button class="layui-btn layui-btn-primary layui-btn-sm" onclick="editFuli(' + item.Id + ')">';
						lihtml += '  <i class="layui-icon layui-icon-edit"></i></button>';
						lihtml += '<button class="layui-btn layui-btn-primary layui-btn-sm" onclick="delFuli(' + item.Id + ')">';
						lihtml += '<i class="layui-icon layui-icon-delete"></i></button>';
						lihtml += '</div></div>';
						lihtml += '<div class="layui-card-body">';
						lihtml += '<img src="' + item.Imgurl + '">';
						lihtml += '</div>';
						lihtml += '</div></li>';
						$("#ulmain").append(lihtml);
					});

				}
			},
			statusCode: {
				401: function() {
					layer.msg("登录信息验证失败，请重新登录");
					location.href = 'login.html';
				}
			},
			complete: function() {
				layer.close(index);
			}

		});

	};

});
//编辑
function editFuli(id) {
	var index = layer.open({
		type: 2,
		title: '福利编辑',
		shadeClose: true,
		shade: 0.8,
		area: ['100%', '100%'],
		content: 'fuli_edit.html?id='+id, //iframe的url
		success: function(layero, index) {
			var body = layer.getChildFrame('body', index);
			body.find("#lblid").val(id);
		},
		end: function() {

		}
	});
};

//删除
function delFuli(id) {
	layer.confirm('确定删除此福利吗？', function(index) { //删除对应行（tr）的DOM结构，并更新缓存
		var index = layer.load(0);
		$.ajax({
			url: apiUrl + 'api/Admin/DelFuli/' + id,
			type: 'post',
			async: true,
			contentType: 'application/json',
			dataType: 'json',
			headers: {
				Authorization: 'Bearer ' + sessionStorage.getItem("token")
			},
			success: function(jsondata) {
				if(jsondata.Type == 1) {
				SearchFulieList(0, 1, 15);
					layer.msg("删除成功");
				} else {
					layer.msg("删除失败");
				}
			},
			complete: function(data, xhr) {
				layer.close(index);
			}

		});
	});

}